package com.contactbook.web.server.service;

import java.util.ArrayList;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.contactbook.web.client.dto.ContactDTO;
import com.contactbook.web.client.service.ContactService;
import com.contactbook.web.server.dto.ContactDTOConverter;
import com.contactbook.web.server.dto.MaritalStatusDTOConverter;

import contactbook.model.Account;
import contactbook.model.Contact;
import contactbook.model.Group;
import contactbook.service.AccountServiceInterface;
import contactbook.service.ContactServiceInterface;
import contactbook.service.GroupServiceInterface;

public class ContactServiceImpl implements ContactService {

	private ContactServiceInterface contactService;
	private AccountServiceInterface accountService;
	private GroupServiceInterface groupService;
	
	public ContactServiceImpl() {
		ServiceLocator sl = ServiceLocator.getInstance();
		contactService = sl.getContactService();
		accountService = sl.getAccountService();
		groupService = sl.getGroupService();
	}

	@Override
	public ContactDTO add(ContactDTO contactDTO, int groupId, String login, String password) {
		Contact contact = new Contact();
		
		// Account
		accountService.login(login, password);
		Account account = accountService.getAccountById(contactDTO.getAccount().getId());
		accountService.logout();
		
		// Group
		// Account
		groupService.login(login, password);
		Group group = groupService.getGroupById(groupId);
		groupService.logout();
		
		contact.setAccount(account);
		if (contactDTO.getAddress() != null)
			contact.setAddress(contactDTO.getAddress());
		
		if (contactDTO.getAccount() != null)
			contact.setAge(contactDTO.getAge());
		
		if (contactDTO.getCountry() != null)
			contact.setCountry(contactDTO.getCountry());
		
		if (contactDTO.getEmail() != null)
			contact.setEmail(contactDTO.getEmail());
		
		if (contactDTO.getAge() != 0)
			contact.setAge(contactDTO.getAge());
		
		contact.setFirstName(contactDTO.getFirstName());
		contact.setLastName(contactDTO.getLastName());
		contact.setGroup(group);
		
		if (contactDTO.getTown() != null)
			contact.setTown(contactDTO.getTown());
		
		if (contactDTO.getMaritalStatus() != null)
			contact.setMaritalStatus(MaritalStatusDTOConverter.convertDTO(contactDTO.getMaritalStatus()));
		
		contactService.login(login, password);
		Contact contactBase = contactService.add(contact);
		contactService.logout();
		
		return ContactDTOConverter.convert(contactBase, true);
	}

	@Override
	public ContactDTO add(ContactDTO contactDTO, String login, String password) {
		Contact contact = new Contact();
		
		// Account
		accountService.login(login, password);
		Account account = accountService.getAccountById(contactDTO.getAccount().getId());
		accountService.logout();
		
		contact.setAccount(account);
		if (contactDTO.getAddress() != null)
			contact.setAddress(contactDTO.getAddress());
		
		if (contactDTO.getAccount() != null)
			contact.setAge(contactDTO.getAge());
		
		if (contactDTO.getCountry() != null)
			contact.setCountry(contactDTO.getCountry());
		
		if (contactDTO.getEmail() != null)
			contact.setEmail(contactDTO.getEmail());
		
		contact.setFirstName(contactDTO.getFirstName());
		contact.setLastName(contactDTO.getLastName());
		
		if (contactDTO.getTown() != null)
			contact.setTown(contactDTO.getTown());
		
		if (contactDTO.getAge() != 0)
			contact.setAge(contactDTO.getAge());
		
		if (contactDTO.getMaritalStatus() != null)
			contact.setMaritalStatus(MaritalStatusDTOConverter.convertDTO(contactDTO.getMaritalStatus()));
		
		contactService.login(login, password);
		Contact contactBase = contactService.add(contact);
		contactService.logout();
		
		return ContactDTOConverter.convert(contactBase, true);
	}

	@Override
	public void delete(int contactId, String login, String password) {
		contactService.login(login, password);
		contactService.delete(contactId);
		contactService.logout();
	}

	@Override
	public ContactDTO getContactById(int contactId, String login, String password) {
		contactService.login(login, password);
		Contact contact = contactService.getContactById(contactId);
		contactService.logout();
		return ContactDTOConverter.convert(contact, true);
	}

	@Override
	public Set<ContactDTO> getContacts(String login, String password) {
		contactService.login(login, password);
		Set<Contact> contacts = new HashSet<Contact>();
		contacts = contactService.getContacts();
		contactService.logout();
		
		Set<ContactDTO> contactsDTO = new HashSet<ContactDTO>();
		for (Contact contact : contacts) {
			contactsDTO.add(ContactDTOConverter.convert(contact, true));
		}
		return contactsDTO;
	}

	@Override
	public Set<ContactDTO> getContacts(int groupId, String login, String password) {
		contactService.login(login, password);
		Set<Contact> contacts = new HashSet<Contact>();
		contacts = contactService.getContacts(groupId);
		contactService.logout();
		
		Set<ContactDTO> contactsDTO = new HashSet<ContactDTO>();
		for (Contact contact : contacts) {
			contactsDTO.add(ContactDTOConverter.convert(contact, true));
		}
		return contactsDTO;
	}

	@Override
	public List<ContactDTO> getContactsByName(String firstName, String lastName, String login, String password) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(ContactDTO contact, String login, String password) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void login(String login, String password) {
		contactService.login(login, password);
	}

	@Override
	public void logout() {
		contactService.logout();
	}

	@Override
	public Set<ContactDTO> getContactsWithoutGroup(String login, String password) {
		contactService.login(login, password);
		List<Contact> contacts = new ArrayList<Contact>();
		contacts = contactService.getContactsWithoutGroup();
		contactService.logout();
		
		Set<ContactDTO> contactsDTO = new HashSet<ContactDTO>();
		for (Contact contact : contacts) {
			contactsDTO.add(ContactDTOConverter.convert(contact, true));
		}
		return contactsDTO;
	}
}
